﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Storage
{
    public partial class OrderMode : Form
    {
        public OrderMode()
        {
            InitializeComponent();
        }
        DataSet set = new DataSet("OrderModeGridTable");
        public void FillOutletCbx(string cmd)
        {
            MySQLDriver drv = MySQLDriver.GetDriver;
            MySqlDataReader reader = drv.Read(cmd);
            while (reader.Read())
            {
                outletCbx.Items.Add(new Outlets(reader.GetInt32(0), reader.GetString(1)).Name);
            }
            drv.CloseConnection();
        }
        public void FillproducerCbx(string cmd)
        {
            MySQLDriver drv = MySQLDriver.GetDriver;
            MySqlDataReader reader = drv.Read(cmd);
            while (reader.Read())
            {
                producerCbx.Items.Add(reader.GetString(0));
            }
            drv.CloseConnection();
        }
        public void FillItemCbx(string cmd)
        {
            MySQLDriver drv = MySQLDriver.GetDriver;
            MySqlDataReader reader = drv.Read(cmd);
            while (reader.Read())
            {
                itemCbx.Items.Add(reader.GetString(0));
            }
            drv.CloseConnection();
        }
        private void OrderMode_Load(object sender, EventArgs e)
        {
            string cmd = "SELECT name FROM producers";
            FillproducerCbx(cmd);
            producerCbx.SelectedItem = 0;
            cmd = "Select name from items where producer_id=" + producerCbx.SelectedIndex;
            FillItemCbx(cmd);
            cmd = "SELECT outlet_id,name from outlets";
            FillOutletCbx(cmd);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void producerCbx_SelectedIndexChanged(object sender, EventArgs e)
        {
            itemCbx.Items.Clear();
            string cmd = "Select name from items where producer_id=" + (producerCbx.SelectedIndex + 1);
            FillItemCbx(cmd);
        }

        private void addButton_Click(object sender, EventArgs e)
        {

            Orders newOrder = new Orders(outletCbx.SelectedIndex,orderDatePicker.Value.ToString("yyyy-MM-dd"));
            OrderProducts newProduct = new OrderProducts(1, newOrder.OrderId, Convert.ToDouble(weightTextBox.Text));
            string cmd = "Insert into orders(outlet_id,date) values('"+newOrder.OutletId+"','"+newOrder.Date+"');";
            MySQLDriver drv = MySQLDriver.GetDriver;
            drv.ExecuteCommand(cmd);
            cmd ="Insert into order_products(item_id,order_id,weight) VALUES ('"+newProduct.ItemId+"','"+newProduct.OrderId+"','"+newProduct.Weight+"');";
            drv.ExecuteCommand(cmd);
            OrderModeSelect();

        }
        private void OrderModeSelect()
        {
            string cmd = @"Select ot.name 'Торговая точка',pr.name 'Производитель',it.name 'Товар',op.weight 'Вес',o.date 'Дата'
            from orders o join order_products op on o.order_id=op.order_id
join outlets ot on ot.outlet_id = o.outlet_id join items it on it.item_id = op.item_id join producers pr on pr.producer_id = it.producer_id ";
            /* @"Select ot.name 'Торговая точка',pr.name 'Производитель',it.name 'Товар',op.weight 'Вес',o.date 'Дата'
            from orders o join order_products op on o.order_id=op.order_id
join outlets ot on ot.outlet_id = o.outlet_id join items it on it.item_id = op.item_id join producers pr on pr.producer_id = it.producer_id ";*/
            MySQLDriver drv = MySQLDriver.GetDriver;
            drv.OpenConnection();
            MySqlCommand comand = new MySqlCommand();
            comand.CommandText = cmd;
            MySqlDataAdapter adapter = new MySqlDataAdapter(cmd,drv.Conn);
            adapter.Fill(set, "OrderModeTable");
            addedGridView.DataMember = "OrderModeTable";
            addedGridView.DataSource = set;
            drv.CloseConnection();
            
        }
    }
}
